export default {
  //添加购物车
  addCart(state,goods){
    //如果已经有该产品,增加数量
    let hasExist = state.cart.some((item)=>{
      if (item.id == goods.id){
        item.num++;
        return true
      }
     });
    if (!hasExist){
      state.cart.push({
        id: goods.id,
        name: goods.name,
        price: goods.price,
        num: 1,
        pic: goods.pic,
        selected: true
      });
    }
    localStorage.setItem('cart',JSON.stringify(state.cart))
  },

  checkOne(state,goods){
    let saveFlag = state.cart.some((item)=>{
      if (item.id == goods.id){
        item.selected=!item.selected;
        return true;
      }
    });
    localStorage.setItem('cart',JSON.stringify(state.cart));
  },

  inc(state,goods){
    state.cart.some(item=>{
      if (item.id == goods.id){
        item.num++;
      }
    });
    localStorage.setItem('cart', JSON.stringify(state.cart));
  },
  dec(state,goods){
    state.cart.some(item=>{
      if (item.id == goods.id){
        item.num--;
      }
    });
    localStorage.setItem('cart', JSON.stringify(state.cart));
  },
  checkAll(state,checked){
    state.cart.forEach(item=>{
      item.selected=checked;
    })
    localStorage.setItem('cart', JSON.stringify(state.cart));
  },
  clearCart(state){
    state.cart=[];
    localStorage.setItem('cart',[]);
  }
}
